package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Department;
import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.Role;
import cn.wolfcode.qo.EmployeeQuery;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.service.IDepartmentService;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.service.IRoleService;
import cn.wolfcode.util.RequiredPermission;
import cn.wolfcode.util.UserContext;
import com.github.pagehelper.PageInfo;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("employee")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;

    @Autowired
    private IDepartmentService departmentService;

    @Autowired
    private IRoleService roleService;

    @RequestMapping("list")
    @RequiredPermission(name = "员工列表",expression ="employee:list")
    public String list(Model model, @ModelAttribute("qo") EmployeeQuery qo){
        PageInfo<Employee> pageInfo = employeeService.query(qo);
        List<Department> departments = departmentService.listAll();
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("departments",departments);
        return "/employee/list";
    }

    @RequestMapping("input")
    @RequiredPermission(name = "员工编辑",expression ="employee:input")
    public String input(Long id,Model model){
        if (id != null){
            Employee employee = employeeService.get(id);
            model.addAttribute("employee",employee);
        }
        List<Department> departments = departmentService.listAll();
        List<Role> roles = roleService.listAll();
        model.addAttribute("departments",departments);
        model.addAttribute("roles",roles);
        return "/employee/input";
    }

    @RequestMapping("saveOrUpdate")
    @RequiredPermission(name = "员工保存或更新",expression ="employee:saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Employee employee,Long[] ids){
            if (employee.getId() != null){
                employeeService.update(employee,ids);
            }
            else {
                employeeService.insert(employee,ids);
            }
            return new JsonResult();
    }

    @RequestMapping("delete")
    @RequiredPermission(name = "员工删除",expression ="employee:delete")
    @ResponseBody
    public JsonResult delete(Long id){
        if (id != null){
            employeeService.delete(id);
        }
        return new JsonResult();
    }

    @RequestMapping("updatePwd")
    public String updatePwd(){
        //获取当前用户信息
        Employee employee = UserContext.getCurrentUser();
        if (employee != null){
        return "/employee/updatePwd";
        }
        return null;
    }

    @RequestMapping("updatePwdById")
    @ResponseBody
    public JsonResult updatePwdByIdAndOldPwd(Long id,String oldPassword,String newPassword){
        if (id != null){
            employeeService.updatePwdByIdAndOldPwd(id,oldPassword,newPassword);
        }
        return new JsonResult();
    }

    @RequestMapping("resetPwd")
    public String resetPwd(Model model,Long id){

        if (id != null){
            Employee employee = employeeService.get(id);
            model.addAttribute("employee",employee);
        }
        return "/employee/resetPwd";
    }

    @RequestMapping("resetPwdByAdmin")
    @ResponseBody
    public JsonResult resetPwdByAdmin(String newPassword,Long id){
        if (id != null ){
            employeeService.resetPwdByAdmin(newPassword,id);
        }
        return new JsonResult();
    }

    //修改状态
    @RequestMapping("changeStatus")
    @ResponseBody
    public JsonResult changeStatus(Long status,Long id){
        if (id != null ){
            employeeService.changeStatus(status,id);
        }
        return new JsonResult();
    }

    //批量删除
    @RequestMapping("batchDelete")
    @ResponseBody
    public JsonResult batchDelete(Long[] ids){
        if (ids.length > 0 && ids !=null) {
            employeeService.batchDelete(ids);
        }
        return new JsonResult();
    }


    //验证用户名
    //插件要求返回结果为键值对形式,key为valid,value为boolean
    //valid 为true,表示验证通过,name不存在
    //valid 为false,表示验证不通过,name存在
    @RequestMapping("checkName")
    @ResponseBody
    public Map<String,Boolean> checkName(String name,Long id){

        HashMap<String,Boolean> map = new HashMap<>();
        Employee employee = employeeService.selectByName(name);
        //如果id为空,执行添加操作验证.
        if (id == null){
           map.put("valid",employee == null);;
        }
        else {
            //id不为空,则为修改操作,验证name
            String oldName = employeeService.get(id).getName();
            if (oldName.equals(name)){
                map.put("valid",true);
            }else {
                map.put("valid",employee == null);
            }
        }
        return map;
    }

    //导出功能
    @RequestMapping("exportXls")
    public void exportXls(HttpServletResponse response) throws IOException {
        //设置文件下载的响应头
        response.setHeader("Content-Disposition","attachment;filename=employee.xls");
        //创建excel文件
        Workbook wb = employeeService.exportXls();
        //把excel的数据输出到浏览器的响应流中
        wb.write(response.getOutputStream());
    }

    //导入功能
    @RequestMapping("importXls")
    @ResponseBody
    public JsonResult importXls(MultipartFile file) throws IOException {
        employeeService.importXls(file);
        return new JsonResult();
    }

    //部门批量查询
    @RequestMapping("deptSearch")
    @ResponseBody
    public JsonResult deptSearch(Long[] ids){


        if (ids != null && ids.length > 0){
            for (Long id : ids){
               Long deptId = employeeService.get(id).getDept().getId();

            }
        }
        return new JsonResult(false,"查询失败");
    }
}
